Skip to content

ArrayList

Introduction to ArrayList

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Represent collections of related object reference data using ArrayList objects.

ESSENTIAL KNOWLEDGE

  • An ArrayList object is mutable and contains object references.

  • The ArrayList constructor ArrayList() constructs an empty list.

  • Java allows the generic type ArrayList<E>, where the generic type E specifies the type of the elements.

  • When ArrayList<E> is specified, the types of the reference parameters and return type when using the methods are type E.

  • ArrayList<E> is preferred over ArrayList because it allows the compiler to find errors that would otherwise be found at run-time.

ArrayList Methods

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Represent collections of related object reference data using ArrayList objects.

ESSENTIAL KNOWLEDGE

  • The ArrayList class is part of the java. util package. An import statement can be used to make this class available for use in the program.

  • The following ArrayList methods— including what they do and when they are used—are part of the Java Quick Reference:

    • int size()-Returnsthenumberof elements in the list
    • boolean add(E obj)-Appends obj to end of list; returns true
    • void add(int index, E obj)- Inserts obj at position index (0 <= index <= size), moving elements at position index and higher to the right (adds 1 to their indices) and adds 1 to size
    • E get(int index)-Returns the element at position index in the list
    • E set(int index, E obj)— Replaces the element at position index with obj; returns the element formerly at position index
    • E remove(int index)—Removes element from position index, moving elements at position index + 1 and higher to the left (subtracts 1 from their indices) and subtracts 1 from size; returns the element formerly at position index

Traversing ArrayLists

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

For ArrayList objects:

  • Traverse using a for or while loop
  • Traverse using an enhanced for loop

ESSENTIAL KNOWLEDGE

  • Iteration statements can be used to access all the elements in an ArrayList. This is called traversing the ArrayList.

  • Deleting elements during a traversal of an ArrayList requires using special techniques to avoid skipping elements.

  • Since the indices for an ArrayList start at 0 and end at the number of elements − 1, accessing an index value outside of this range will result in an IndexOutOfBoundsException being thrown.

  • Changing the size of an ArrayList while traversing it using an enhanced for loop can result in a ConcurrentModificationException being thrown. Therefore, when using an enhanced for loop to traverse an ArrayList, you should not add or remove elements.

Developing Algorithms Using ArrayLists

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

For algorithms in the context of a particular specification that requires the use of ArrayList traversals:

  • Identify standard algorithms.
  • Modify standard algorithms.
  • Develop an algorithm.

ESSENTIAL KNOWLEDGE

  • There are standard ArrayList algorithms that utilize traversals to:

    • Insert elements
    • Delete elements
    • Apply the same standard algorithms that are used with 1D arrays
  • Some algorithms require multiple String, array, or ArrayList objects to be traversed simultaneously.

Searching

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Apply sequential/linear search algorithms to search for specific information in array or ArrayList objects.

ESSENTIAL KNOWLEDGE

  • There are standard algorithms for searching.

  • Sequential/linear search algorithms check each element in order until the desired value is found or all elements in the array or ArrayList have been checked.

Sorting

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Apply selection sort and insertion sort algorithms to sort the elements of array or ArrayList objects.

ESSENTIAL KNOWLEDGE

  • Selection sort and insertion sort are iterative sorting algorithms that can be used to sort elements in an array or ArrayList.

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Compute statement execution counts and informal run-time comparison of sorting algorithms.

ESSENTIAL KNOWLEDGE

  • Informal run-time comparisons of program code segments can be made using statement execution counts.

Ethical Issues Around Data Collection

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Explain the risks to privacy from collecting and storing personal data on computer systems.

ESSENTIAL KNOWLEDGE

  • When using the computer, personal privacy is at risk. Programmers should attempt to safeguard personal privacy.

  • Computer use and the creation of programs have an impact on personal security. These impacts can be beneficial and/or harmful.